package com.cuit.bysj.service;

import java.util.Date;
import java.util.List;

import com.cuit.bysj.dao.UserDao;
import com.cuit.bysj.entities.Appraise;
import com.cuit.bysj.entities.Student;
import com.cuit.bysj.entities.Subject;
import com.cuit.bysj.entities.Teacher;
import com.cuit.bysj.entities.User;
import com.cuit.bysj.utils.DateUtils;

public class UserService {
	private UserDao userDao;

	public void setUserDao(UserDao UserDao) {
		this.userDao = UserDao;
	}

	// 判断登录是否成功，成功返回用户实体
	public User isLogin(String username, String password) {
		User user = userDao.findUser(username, password);

		if (user != null) {

			return user;
		}

		return null;
	}

	// 根据当前登录的学生对象创建他所有需要评价的评教表
	@SuppressWarnings("rawtypes")
	public void initform(Student stu) {
		List list1 = userDao.loadlist("from Appraise a where a.student=" + stu.getId());

		if (list1.size() == 0) {
			//查询当前学生在选课表中需要评价的老师列表hql语句
			String hql = "select x.teacher,x.subject from Xuanke x where x.student =" + stu.getId();

			List list = userDao.loadlist(hql);
			
			for (int i = 0; i < list.size(); i++) {
				Object obj[] = (Object[]) list.get(i);
				Teacher t = (Teacher) obj[0];
				Subject s = (Subject) obj[1];
				Appraise appraise = new Appraise(t, s, stu);
				appraise.seteTime(DateUtils.getDateAfter(new Date(), 16));
				appraise.setsTime(DateUtils.getDateAfter(new Date(), 0));
				userDao.addTable(appraise);
			}
		}
	}

	public void updateUser(User user) {
		userDao.update(user);
	}

	// 管理员登录时向数据库中插入一条数据，并且默认表示为当前关闭评教状态
	public void initState() {
		userDao.initState();
	}
	
	public User getUserbyid(Integer id){
		return userDao.getserByID(id);
	}
}
